layui.use(['form','table','element','laytpl','layer','upload','treeGrid'], function() {
  var table = layui.table;
  var upload = layui.upload;
  var treeGrid = layui.treeGrid;
/*  table.render({
    elem: '#supplierTable'
    ,url:'/ssm/classes/queryList.do'
    ,cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
    ,page : true
    ,height : "full-125"
    ,cols: [[
      {field:'cid', width:80, title: 'ID',type:'numbers'}
      ,{field:'cname', title: '分类名称'}
      ,{align:'center', toolbar: '#barTpl', width: 120, title: '操作'}
    ]]
  });*/
  
  var treeTable =treeGrid.render({
	    elem: "#treeMenuTable",
	    url:"/ssm/classes/queryList.do",
	    cellMinWidth: 100,
	    treeId:"cid",//树形id字段名称
	    treeUpId:"pid",//树形父id字段名称
	    treeShowName:"cname",//以树形式显示的字段
	    id:"treeMenuTable",
	    cols : [[
	        {field:'cname', title: '分类名称'}
	        ,{align:'center', toolbar: '#barTpl', width: 120, title: '操作'}
	     ]],
	    page:false
	});
  

//工具条点击事件
	treeGrid.on('tool(treeMenuTable)', function(obj){
		var data = obj.data;
  		var layEvent = obj.event;

  		if(layEvent === 'edit'){ //修改
  			showEditModel(data);
  		} else if(layEvent === 'del'){ //删除
  			doDelete(data);
  		} else if(layEvent == 'detail'){
  			showPermDialog(data.id);
  		}else if(layEvent == 'operateAuth'){
  			perAuthWin(data.id,data.role_name);
  		}
	});
  
  

  	//添加按钮点击事件
  	$("#addBtn").click(function(){
  		showEditModel(null);
  		return false;
  	});

  	$(".search_btn").on("click", function() {
        treeGrid.reload("treeMenuTable", {
            page : {
                curr : 1
            },
            where : {},
            page:false
        })
    });

  	//显示表单弹窗
    function showEditModel(data){
    	cid = data==null?'':data.cid;
        layer.open({
            type: 2,
            title: data==null?"添加":"修改",
    		area:['60%','80%'],
            offset: 'auto',
            content: '/ssm/system/classes/edit.html?cid='+cid
        });
        if(data!=null){
            $("#editForm input[id=cid]").val(data.cid);
            $("#editForm input[id=cname]").val(data.cname);
        }
        $("#btnCancel").click(function(){
            layer.closeAll('page');
        });

    }

    	//表单提交事件
    layui.form.on('submit(btnSubmit)', function(data){
            var qUrl="/ssm/classes/save.do";
            $.post(qUrl,{
                cid : $("#cid").val(),
                cname : $("#cname").val(),
            },function(res){
             if(res.code=="01"){
                 layer.closeAll();
                 layer.msg("保存成功");
                 //刷新父页面
                 layui.table.reload('supplierTable', {});
             }else{
                 layer.msg("保存失败");
             }
         });
            return false;
    });

    
	//删除
	function doDelete(data){
        layer.confirm('确定删除吗？',{icon:3, title:'提示信息'},function(index){
            $.post("/ssm/classes/del.do",{
            		id : data.cid
            },function(data){
                if(data.code=="01"){
                	layer.close(index);
                	treeGrid.reload("treeMenuTable", {});
                    
                }else{
                	layer.close(index);
                    layer.alert(data.body);
                    
                }

            })
        });
	}

});